from knglib.baselib import checkVariableType

class Section:
    def __init__(self, name, comment=None):
        self.name = name
        self.comment = comment
        self.__data = {}
        self.__comments = {}
        self.__lines = []
    
    def set(self, option, value, comment=None):
        checkVariableType('option', str, option)
        self.__data[option] = value
        self.__comments[option] = comment
    
    def addLine(self, line):
        self.__lines.append(line)
    
    def getLines(self):
        return self.__lines
    
    def get(self, option):
        checkVariableType('option', str, option)
        return self.__data.get(option)
    
    def getOptionsName(self):
        return self.__data.keys()
    
    def getComment(self, option):
        checkVariableType('option', str, option)
        return self.__comments.get(option)
    
    def getValue(self, option):
        checkVariableType('option', str, option)
        return self.__data.get(option)
    
    def getLine(self, option):
        checkVariableType('option', str, option)
        return OptionLine(option, self.__data.get(option), self.__comments.get(option))
    
    def has_option(self, option):
        checkVariableType('option', str, option)
        return self.__data.has_key(option)


class SectionGroup:
    def __init__(self):
        self.__group = {}
    
    def add(self, section):
        checkVariableType('section', Section, section)
        name = section.name
        if self.__group.has_key(name):
            raise Warning('Overwriting section duplicated \'%s\'' % name)
        self.__group[name] = section
    
    def get(self, sectionname):
        return self.__group.get(sectionname)
    
    def getGroup(self):
        return self.__group
    
    def getNames(self):
        return self.__group.keys()
    
    def length(self):
        return size(self.__group)
    
    def has_sectionName(self, name):
        return self.__group.has_key(name)
